package com.qf.service.impl;

import com.qf.entity.Address;
import com.qf.entity.UserAdderss;
import com.qf.mapper.AddressMapper;
import com.qf.mapper.UserAdresssMapper;
import com.qf.service.IAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lcx
 * @since 2020-09-03
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {
    @Autowired
    Jedis jedis;
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    UserAdresssMapper userAdresssMapper;

    @Override
    public void updateAddress(Address address) {
        addressMapper.updateById(address);
    }

    @Override
    public void addAddress(Address address,Integer uid) {
        addressMapper.insert(address);
        UserAdderss userAdderss=new UserAdderss(address.getId(),uid);
        userAdresssMapper.addAdd(userAdderss);

    }

    @Override
    public List<Address> getAddress(Integer uid) {
        List<Integer> integerList = userAdresssMapper.selectAid(uid);
        List<Address> list=new ArrayList();
        for (Integer aid : integerList) {
            Address address = addressMapper.addressSelect(aid);
            list.add(address);
        }
       return list;
    }

    @Override
    public Integer getId(String token){
        Integer id  = Integer.valueOf(jedis.get(token));
        return id;
    }

}
